package com.ziq.baselib.test.databinding

import android.os.Bundle
import android.util.Log
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.service.ConnectInfo
import com.ziq.base.service.ILongLinkListener
import com.ziq.base.service.LongLinkServiceWarp
import com.ziq.base.service.MessageProto
import com.ziq.baselib.databinding.ActivityDataBindingBinding

class DataBindingActivity: BaseActivity() {
    private val binding : ActivityDataBindingBinding by binding()

    var mLongLinkServiceWarp: LongLinkServiceWarp? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.user = User("ziq =========", 100)
        binding.btnLongLinkService.click {
            mLongLinkServiceWarp = LongLinkServiceWarp(context = this)
            mLongLinkServiceWarp?.init()
        }
        binding.btnLongLink.click {
            mLongLinkServiceWarp?.connect(ConnectInfo().apply {
                url = "ws://172.16.3.155:3102/sub"
                uid = 1234
            })
            mLongLinkServiceWarp?.registerListener(object : ILongLinkListener.Stub() {
                override fun onReceive(originBytes: ByteArray?, body: ByteArray?) {
                    Log.i("wyq", "${Thread.currentThread().name} $originBytes -- ${String(body?: ByteArray(0))}")
                }
            })
        }
        binding.btnLongLinkDis.click {
            mLongLinkServiceWarp?.disconnect()
        }
        binding.btnLongLinkSend.click {
            val bodyByteArray = "test".toByteArray()
            val message = MessageProto()
            message.version = 1
            message.op = 2
            message.seq = 1
            message.body = bodyByteArray
            mLongLinkServiceWarp?.send(message.getProtoByteArray())
        }
    }
}